package com.huawei.lynnbit.tool.cocos;

import com.huawei.lynnbit.tool.cocos.config.SystemConfig;
import com.huawei.lynnbit.tool.cocos.config.game.GameConfig;
import com.huawei.lynnbit.tool.cocos.config.game.task.Stage;
import com.huawei.lynnbit.tool.cocos.processor.CompositeProcessor;
import com.huawei.lynnbit.tool.cocos.processor.Processor;
import com.huawei.lynnbit.tool.cocos.processor.preset.AppProcessor;
import com.huawei.lynnbit.tool.cocos.processor.preset.ExportProcessor;
import com.huawei.lynnbit.tool.cocos.processor.preset.PackageProcessor;
import com.huawei.lynnbit.tool.cocos.util.CmdUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class FlowController {
    public static void main(String[] args) {
        try {
            log.info("start to read config files >>>>>>>>>>>>>>>>>>>>>>>>");
            SystemConfig systemConfig = SystemConfig.newInstance("systemconfig.json");
            if (!systemConfig.isValid()) {
                log.error("systemConfig is not valid");
                return;
            }

            List<GameConfig> gameConfigs = GameConfig.newInstance("gameconfig.json");
            for (GameConfig config : gameConfigs) {
                if (!config.isValid()) {
                    log.error("gameConfig is not valid");
                    return;
                }

                config.setSystemConfig(systemConfig);
            }

            log.info("start to set environment >>>>>>>>>>>>>>>>>>>>>>>>");
            CmdUtils.initEnv(systemConfig.getDevEcoInstallPath());

            log.info("start to work >>>>>>>>>>>>>>>>>>>>>>>>");
            for (GameConfig config : gameConfigs) {
                if (!config.isNeedRun()) {
                    continue;
                }
                Processor root = new CompositeProcessor();
                if (CollectionUtils.isNotEmpty(config.getTaskInfo())) {
                    Processor compositeProcessor = new CompositeProcessor(config.getTaskInfo().stream().filter(task -> Stage.BEFORE_EXPORT.equals(task.getStage())).collect(Collectors.toList()), config);
                    root.addProcessor(compositeProcessor);
                }

                if (!StringUtils.isAllBlank(systemConfig.getCocosCreator2Path(), systemConfig.getCocosCreator3Path())) {
                    root.addProcessor(new ExportProcessor(config));
                }
                root.addProcessor(new AppProcessor(config));

                if (CollectionUtils.isNotEmpty(config.getTaskInfo())) {
                    Processor compositeProcessor = new CompositeProcessor(config.getTaskInfo().stream().filter(task -> Stage.BEFORE_PACKAGE.equals(task.getStage())).collect(Collectors.toList()), config);
                    root.addProcessor(compositeProcessor);
                }

                root.addProcessor(new PackageProcessor(config));
                root.process();
            }
            log.info("finish the work >>>>>>>>>>>>>>>>>>>>>>>>");
        } catch (Exception e) {
            log.error("running exception: ", e);
        }
    }
}
